function [y_bar,acc,y_prob] = nnpredict(Theta1, Theta2, X ,y)
%PREDICT Predict the label of an input given a trained neural network
%   [p acc] = nnPREDICT(Theta1, Theta2, X) outputs the predicted label of X given the
%   trained weights of a neural network (Theta1, Theta2)
%   acc is the accuracy for predict y

% Useful values
if nargin==3 && ~isa(Theta1,'struct')
    y = zeros(size(X,1),1);
elseif nargin==3 && isa(Theta1,'struct')
    y = X;
    X = Theta2;
    Theta2 = Theta1.theta2;
    Theta1 = Theta1.theta1;
elseif nargin==2 && isa(Theta1,'struct')
    X = Theta2;
    y = zeros(size(X,1),1);
    Theta2 = Theta1.theta2;
    Theta1 = Theta1.theta1;
end

m = size(X, 1);

% You need to return the following variables correctly 

h1 = sigmoid([ones(m, 1) X] * Theta1');
h2 = sigmoid([ones(m, 1) h1] * Theta2');
[~, y_bar] = max(h2, [], 2);

if nargout>1
    acc = mean(y==y_bar);
    y_prob = h2./repmat(sum(h2,2),1,size(h2,2));
end

% =========================================================================


end
